package com.bluegalaxy.sitescore.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bluegalaxy.sitescore.common.enums.PermissionType;
import com.bluegalaxy.sitescore.common.enums.RecordStatus;
import com.bluegalaxy.sitescore.entity.RolePermission;
import com.bluegalaxy.sitescore.service.IPermissionService;
import com.bluegalaxy.sitescore.service.IRolePermissionService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.bluegalaxy.sitescore.entity.Permission;
import com.bluegalaxy.sitescore.service.IPermissionService;
import com.bluegalaxy.sitescore.service.IRolePermissionService;

import java.util.List;

/**
 * <p>
 * 角色管理相关操作
 * </p>
 *
 *
 * @Author
 * @Date 2016-04-15
 */
@Controller
@RequestMapping("/perm/permission")
public class PermissionController extends BaseController {

	@Autowired
	private IPermissionService permissionService;

	@Autowired
	private IRolePermissionService rolePermissionService;

	@com.baomidou.kisso.annotation.Permission("2003")
	@RequestMapping("/list")
	public String list(Model model) {
		return "/permission/list";
	}

	@ResponseBody
	@com.baomidou.kisso.annotation.Permission("2003")
	@RequestMapping("/getPermissionList")
	public String getPermissionList() {
		Page<Permission> page = getPage();
		EntityWrapper<Permission> entityWrapper = new EntityWrapper<>();
		entityWrapper.where("state!="+RecordStatus.Invalid.getValue());
		return jsonPage(permissionService.selectPage(page, entityWrapper));
	}
	@com.baomidou.kisso.annotation.Permission("2001")
	@RequestMapping("/edit")
	public String edit(Model model, Long id ) {
		Permission permission=new Permission();
		permission.setId((long)0);
		permission.setTitle("根目录");
		List<Permission>  permissionList= Lists.newArrayList();
		permissionList.add(permission);
		permissionList.addAll(permissionService.selectPermissionTopList());
		model.addAttribute("permissionTopList",permissionList);
		if ( id != null ) {

			model.addAttribute("permission", permissionService.selectById(id));
		}
		return "/permission/edit";
	}

	@ResponseBody
	@com.baomidou.kisso.annotation.Permission("2003")
	@RequestMapping("/delete/{permId}")
	public String delete(@PathVariable Long permId) {
		Permission permission = permissionService.selectById(permId);
		RolePermission rolePermission = new RolePermission();
		rolePermission.setPid(permId);
		if (permission==null) {
			return "false";
		}
		permission.setState(RecordStatus.Invalid.getValue());
		if(rolePermissionService.selectCount(rolePermission)==0){
			if (permissionService.updateById(permission)) {
				return "true";
			} else {
				return "false";
			}
		}else {
			if (rolePermissionService.deleteSelective(rolePermission) && permissionService.updateById(permission)) {
				return "true";
			} else {
				return "false";
			}
		}

	}

	@ResponseBody
	@com.baomidou.kisso.annotation.Permission("2001")
	@RequestMapping("/editPerm")
	public String editUser( Permission permission ) {
		boolean rlt = false;
		if (permission != null ) {
			if ( permission.getId() != null ) {
				rlt = permissionService.updateSelectiveById(permission);
			} else {
				permission.setState(RecordStatus.Effective.getValue());
				permission.setDescription(permission.getTitle());
				permission.setPermCode("2000");
				permission.setType(PermissionType.MENU.key());//设置为菜单类型
				rlt = permissionService.insert(permission);
				Long id = permission.getId();
				RolePermission rolePermission = new RolePermission();
				rolePermission.setPid(permission.getId());
				rolePermission.setRid(Long.valueOf(1));//设置系统管理员id
				rlt = rolePermissionService.insert(rolePermission);
			}
		}
		return callbackSuccess(rlt);
	}

}
